package org.jeecg.modules.business.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.business.dto.BusMeetingRoomScheduleDetailDto;
import org.jeecg.modules.business.dto.BusMeetingRoomScheduleDto;
import org.jeecg.modules.business.dto.PostDataDto;
import org.jeecg.modules.business.entity.*;
import org.jeecg.modules.business.enums.CouponsStatusEnum;
import org.jeecg.modules.business.service.IBusMarketCouponsEventService;
import org.jeecg.modules.business.service.IBusMeetingRoomScheduleDetailService;
import org.jeecg.modules.business.service.impl.BusMeetingRoomScheduleDetailServiceImpl;
import org.jeecg.modules.business.service.impl.BusMeetingRoomScheduleServiceImpl;
import org.jeecg.modules.business.service.impl.BusMeetingRoomServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: jeecg-boot
 * @Date:   2023-03-16
 * @Version: V1.0
 */
@RestController
@RequestMapping("/business/busMeetingRoomScheduleDetail")
public class BusMeetingRoomScheduleDetailController extends JeecgController<BusMeetingRoomScheduleDetail, IBusMeetingRoomScheduleDetailService> {

    @Resource
    private BusMeetingRoomScheduleDetailServiceImpl meetingRoomScheduleDetailService;
    @Resource
    private BusMeetingRoomScheduleServiceImpl meetingRoomScheduleService;
    @Resource
    private BusMeetingRoomServiceImpl meetingRoomService;

    /**
     * 分页列表查询
     *
     * @param busMeetingRoomScheduleDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BusMeetingRoomScheduleDetail>> queryPageList(BusMeetingRoomScheduleDetail busMeetingRoomScheduleDetail,
                                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                     HttpServletRequest req) {
        QueryWrapper<BusMeetingRoomScheduleDetail> queryWrapper = QueryGenerator.initQueryWrapper(busMeetingRoomScheduleDetail, req.getParameterMap());
        Page<BusMeetingRoomScheduleDetail> page = new Page<BusMeetingRoomScheduleDetail>(pageNo, pageSize);
        IPage<BusMeetingRoomScheduleDetail> pageList = meetingRoomScheduleDetailService.page(page, queryWrapper);
        pageList.getRecords().forEach(item -> {
            BusMeetingRoomSchedule meetingRoomSchedule = meetingRoomScheduleService.getById(item.getMeetingRoomScheduleId());
            if (meetingRoomSchedule != null) {
                item.setTheme(meetingRoomSchedule.getTheme());
            }
            BusMeetingRoom meetingRoom = meetingRoomService.getById(item.getMeetingRoomId());
            if (meetingRoom != null) {
                item.setMeetingRoomName(meetingRoom.getName());
            }
        });
        return Result.OK(pageList);
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "bus_market_coupons_cash_info-通过id删除")
    @ApiOperation(value="bus_market_coupons_cash_info-通过id删除", notes="bus_market_coupons_cash_info-通过id删除")
    //@RequiresPermissions("business:bus_market_coupons_cash_info:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        meetingRoomScheduleDetailService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "bus_market_coupons_cash_info-批量删除")
    @ApiOperation(value="bus_market_coupons_cash_info-批量删除", notes="bus_market_coupons_cash_info-批量删除")
    //@RequiresPermissions("business:bus_market_coupons_cash_info:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.meetingRoomScheduleDetailService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     *  批量确认
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "bus_market_coupons_cash_info-批量确认")
    @ApiOperation(value="bus_market_coupons_cash_info-批量确认", notes="bus_market_coupons_cash_info-批量确认")
    @PostMapping(value = "/confirmBatch")
    public Result<String> confirmBatch(@RequestBody PostDataDto dto) {
        LambdaUpdateWrapper<BusMeetingRoomScheduleDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(BusMeetingRoomScheduleDetail::getId, dto.getIds());
        updateWrapper.set(BusMeetingRoomScheduleDetail::getStatus, 1);
        meetingRoomScheduleDetailService.update(updateWrapper);
        return Result.OK("批量确认成功!");
    }

    /**
     *  确认
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "bus_market_coupons_cash_info-确认")
    @ApiOperation(value="bus_market_coupons_cash_info-确认", notes="bus_market_coupons_cash_info-确认")
    @PostMapping(value = "/confirm")
    public Result<String> confirm(@RequestBody PostDataDto dto) {
        LambdaUpdateWrapper<BusMeetingRoomScheduleDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BusMeetingRoomScheduleDetail::getId, dto.getId());
        updateWrapper.set(BusMeetingRoomScheduleDetail::getStatus, 1);
        meetingRoomScheduleDetailService.update(updateWrapper);
        return Result.OK("确认成功!");
    }

    /**
     *  确认
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "bus_market_coupons_cash_info-确认")
    @ApiOperation(value="bus_market_coupons_cash_info-确认", notes="bus_market_coupons_cash_info-确认")
    @PostMapping(value = "/confirmBatchByMeetingRoomScheduleId")
    public Result<String> confirmBatchByMeetingRoomScheduleId(@RequestBody PostDataDto dto) {
        LambdaUpdateWrapper<BusMeetingRoomScheduleDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BusMeetingRoomScheduleDetail::getMeetingRoomScheduleId, dto.getId());
        updateWrapper.set(BusMeetingRoomScheduleDetail::getStatus, 1);
        meetingRoomScheduleDetailService.update(updateWrapper);
        return Result.OK("确认成功!");
    }

}

